﻿using Assets.Core.Common.Web;
using Assets.Core.Jwt;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nucleic.Core.DoMain;
using Nucleic.Core.Utils;
using Nucleic.Web.Models.Account;

namespace Nucleic.Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        /// <summary>
        /// 操作员登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Authenticate")]
        public Result Authenticate(AuthenticateInput input)
        {
          var oper=  SqlSugarFactory.Instance.Queryable<@operator>()     
                .Where(t1 => t1.oper_id == input.oper_id && t1.oper_pwd == input.oper_pwd)
                .First();
            if (oper==null)
            {
                return Result.fail("用户名或密码错误");
            }
            if (oper.status!="0")
            {
                return Result.fail("用户禁止登录");

            }
            var type= SqlSugarFactory.Instance.Queryable<operator_type>()
                .Where(t1 => t1.oper_type == oper.oper_type)
                .First();
            if (type==null)
            {
                return Result.fail("用户禁止登录");
            }
            if (type.status!="0")
            {
                return Result.fail("用户禁止登录");
            }
            //生成toen
            var payload = new Dictionary<string, object>
                {
                    { "id",oper.oper_id },
                    { "name",oper.name },
                    { "iatTime", DateTime.Now },
                    //{ "expTime", DateTime.Now.AddHours(1)},
                    { "expTime", DateTime.Now.AddDays(1)},
                };
            //AbpSession
            var token = JwtHelp.SetJwtEncode(payload);
            
            //var tokena= Request.Headers["token"].FirstOrDefault();
            //var c = JwtHelp.CheckToken(token);
            
            return Result.success(token);
        }
    }
}
